﻿using System.Data;
using System.Reflection;

namespace Koala.Pro.Common
{
    public static class ExtTable
    {
        public static DataTable GetPage(this DataTable data, int pageIndex, int pageSize, out int allPage)
        {
            allPage = data.Rows.Count / pageSize;
            allPage += ((data.Rows.Count % pageSize != 0) ? 1 : 0);
            DataTable dataTable = data.Clone();
            int num = pageIndex * pageSize;
            int num2 = ((num + pageSize > data.Rows.Count) ? data.Rows.Count : (num + pageSize));
            if (num < num2)
            {
                for (int i = num; i < num2; i++)
                {
                    dataTable.ImportRow(data.Rows[i]);
                }
            }
            return dataTable;
        }

        public static DataTable GetDataFilter(this DataTable data, string condition)
        {
            if (data != null && data.Rows.Count > 0)
            {
                if (condition.Trim() == "")
                {
                    return data;
                }
                DataTable dataTable = new DataTable();
                dataTable = data.Clone();
                DataRow[] array = data.Select(condition);
                for (int i = 0; i < array.Length; i++)
                {
                    dataTable.ImportRow(array[i]);
                }
                return dataTable;
            }
            return null;
        }

        public static DataTable GetSelectTop(this DataTable data, int top)
        {
            if (data.Rows.Count < top)
            {
                return data;
            }
            DataTable dataTable = data.Clone();
            DataRow[] array = data.Select("1=1");
            for (int i = 0; i < top; i++)
            {
                dataTable.ImportRow(array[i]);
            }
            return dataTable;
        }

        public static DataTable ToDataTable<T>(this IEnumerable<T> list)
        {
            List<PropertyInfo> pList = new List<PropertyInfo>();
            Type typeFromHandle = typeof(T);
            DataTable dt = new DataTable();
            Array.ForEach(typeFromHandle.GetProperties(), delegate (PropertyInfo p)
            {
                pList.Add(p);
                dt.Columns.Add(p.Name);
            });
            foreach (T item in list)
            {
                DataRow row = dt.NewRow();
                pList.ForEach(delegate (PropertyInfo p)
                {
                    row[p.Name] = p.GetValue(item, null);
                });
                dt.Rows.Add(row);
            }
            return dt;
        }

        public static Dictionary<string, object> ToDict(this DataTable dt)
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                dictionary[dt.Columns[i].ColumnName] = Convert.ToString(dt.Rows[0][dt.Columns[i].ColumnName]);
            }
            return dictionary;
        }

        public static List<Dictionary<string, object>> ToDictList(this DataTable dt)
        {
            return EnumerableRowCollectionExtensions.Select(DataTableExtensions.AsEnumerable(dt), (DataRow row) => dt.Columns.Cast<DataColumn>().ToDictionary((DataColumn column) => column.ColumnName, (DataColumn column) => row[column])).ToList();
        }
    }
}
